<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/14
 * Time: 22:18
 */
namespace app\model;


use think\facade\Db;
use think\Model;
class Leave extends Model
{
    protected $table = 'user_leave';
    protected $pk = 'leave_id';

    const APPROVAL_WITHDRAW = 0;
    const APPROVAL_HEAD = 1;
    const APPROVAL_CHARGE = 2;
    const APPROVAL_PASS = 3;
    const APPROVAL_REJECT = 4;

    private function _getListField(){
        return [
            'leave_id' => 'leaveId',
            'pername' => 'pername',
            'leave_type' => 'type',
            'leave_approval' => 'approval',
            'leave_reason' => 'reason',
            'leave_remark' => 'remark',
            'leave_start_time' => 'startTime',
            'leave_end_time' => 'endTime'
        ];
    }

    public function getLeaveInfoById($leaveId,$field = ''){

        $field = $field ?: self::_getListField();
        $where = [
           'leave_id' => $leaveId
        ];

        $info = Db::table('user_leave')
            ->leftJoin('user','id=leave_uid')
            ->where($where)
            ->field($field)
            ->find();
        if($info){
            $info['startDay'] = date('Y/m/d',$info['startTime']);
            $info['startHour'] = date('H:i',$info['startTime']);
            $info['endDay'] = date('Y/m/d',$info['endTime']);
            $info['endHour'] = date('H:i',$info['endTime']);
            return $info;
        }
        return false;
    }
    public function addOneLeave($data){
        $data['leave_create_time'] = time();
        return Db::table('user_leave')->insertGetId($data);
    }

    public function checkIfLeave($uid,$startTime,$endTime){

        $where = "leave_approval in(1,2) and leave_uid={$uid} and ((leave_start_time>={$startTime} and leave_end_time<={$startTime}) or (leave_start_time>={$endTime} and leave_end_time<={$endTime}))";
        $rt = Db::table('user_leave')
            ->where($where)
            ->find();
        return $rt ? true : false;
    }

    public function getLeaveListByCondition($where = [],$order = 'leave_id desc',$field = ''){
        $field = $field ?: self::_getListField();
        $list = Db::table('user_leave')
            ->leftJoin('user','id=leave_uid')
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate(input('param.pageSize'))
            ->each(function ($item,$key){
                $item['startDay'] = date('Y/m/d',$item['startTime']);
                $item['startHour'] = date('H:i',$item['startTime']);
                $item['endDay'] = date('Y/m/d',$item['endTime']);
                $item['endHour'] = date('H:i',$item['endTime']);
                return $item;
            })
            ->render();
        return $list;
    }

    public function getLeaveListByWhere($where = [],$order = 'leave_id desc',$field = ''){
        $field = $field ?: self::_getListField();
        $list = Db::table('user_leave')
            ->leftJoin('user','id=leave_uid')
            ->where($where)
            ->field($field)
            ->order($order)
            ->select();
        return $list;
    }

    public function updateLeaveByWhere($where,$data){
        $data['leave_update_time'] = time();
        return Db::table('user_leave')->where($where)->update($data);
    }
}